package com.mobile.business.grid;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import com.mobile.model.EduHallData;
import com.mobile.tools.CopyFile;
import com.mobile.tools.FieldInfo;
import com.mobile.tools.HibernateEntityManager;
import com.mobile.tools.HibernateMappingManager;
import com.mobile.tools.exception.FileIsExistsException;
import com.mobile.tools.exception.FilePathException;

public class EduHallDataDDLBusiness {
	//hibernate文件的名称
	private String configName="hibernate.cfg.xml";
	private HibernateEntityManager hem;
	private HibernateMappingManager hmm;
	//新实体在hibernate里面的名称
	private String entityName;
	//实体类中动态映射map的属性名称
	private String componentName;
	//新的映射文件的名称
	private String mappingName="EduHallData02.hbm.xml";
	//复制模板Mapping文件的名称
	private String mappingCopyTemplate="EduHallData01.hbm.xml";
	
	
	public EduHallDataDDLBusiness(String entityName,String componentName,String mappingName){
		this.entityName=entityName;
		this.componentName=entityName;
		this.mappingName=mappingName;
		this.hem=new HibernateEntityManager(entityName,componentName,mappingName);
		this.hmm=new HibernateMappingManager();
	}
	public EduHallDataDDLBusiness(String configName,String entityName,String componentName,String mappingName,String mappingCopyTemplate){
		this.componentName=configName;
		this.entityName=entityName;
		this.componentName=entityName;
		this.mappingName=mappingName;
		this.mappingCopyTemplate=mappingCopyTemplate;
		/*this.hem=new HibernateEntityManager(entityName,componentName,mappingName);*/
		this.hmm=new HibernateMappingManager();
	}
	public HibernateEntityManager getHem() {
		return hem;
	}
	public void setHem(HibernateEntityManager hem) {
		this.hem = hem;
	}
	public String getConfigName() {
		return configName;
	}
	public void setConfigName(String configName) {
		this.configName = configName;
	}
	/*public List<EduHallData> findByExample(String entityName,EduHallData ehd){
		return (List<EduHallData>)bdao.findByExample(entityName, ehd);
	}
	public void save(String entityName){
		for(int i=0;i<50;i++){
			EduHallData ehd=new EduHallData();
			ehd.setSysDeptId("deptId");
			ehd.setSysUserInfoId("userId");
			bdao.save(entityName, ehd);
		}
	}
	public void save(){
		EduHallData ehd=new EduHallData();
		ehd.setSysDeptId("deptId01");
		ehd.setSysUserInfoId("userId01");
		ehd.setDataPropertiesField("fld_email", "asdfd");
		bdao.save("EDU_HALL_DATA_01", ehd);
	}*/
	/**
	 * 创建表格并添加字段
	 */
	public void createEduHallDataTable(List<FieldInfo> fieldInfo)
		throws FilePathException,FileIsExistsException,URISyntaxException{
		String mappingCopyTemplate=getMappingCopyTemplate();
		//映射文件的名称
		String mappingName=getMappingName();
		//新的实体名称
		String entityName="EDU_HALL_DATA_02";
		//模板路径
		URI source=EduHallData.class.getResource(mappingCopyTemplate).toURI();
		
		//创建mapping文件
		CopyFile.copyFile(mappingName ,source);
	
		//HibernateMappingManager hmm=new HibernateMappingManager();
		//修改hibernate配置文件
		hmm.updateEntityName(mappingName,entityName);
		//hmm.updateHibernateConfig(configName,mappingName);
		//添加新字段
		//HibernateEntityManager hem=new HibernateEntityManager(entityName,"dataProperties",mappingName);
		//Configuration con=HibernateSessionFactory.getConfiguration();
		
		hem.addEntityField(fieldInfo);
		/*HibernateSessionFactory.reset();
		HibernateSessionFactory.rebuildSessionFactory();
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = session.beginTransaction();
		try {

			EduHallData ehd = new EduHallData();
			ehd.setSysDeptId("deptId01");
			ehd.setSysUserInfoId("userId01");
			ehd.setDataPropertiesField("email", 100l);
			Serializable id = session.save(entityName,ehd);
			tx.commit();

		} catch (Exception e) {
			tx.rollback();
			System.out.println("e = " + e);
		}*/
	}
	/**
	 * 创建表格
	 * @throws FilePathException
	 * @throws FileIsExistsException
	 * @throws URISyntaxException
	 */
	public void createEduHallDataTable()
			throws FilePathException,FileIsExistsException,URISyntaxException{
		
		//路径
		URI source=EduHallData.class.getResource(this.mappingCopyTemplate).toURI();
		
		
		
		//创建mapping文件
		CopyFile.copyFile(this.mappingName, source);
		//修改新的配置文件在hibernate的config的名称
		this.hmm.updateEntityName(this.mappingName, entityName);
		//修改hibernate 的配置文件
		//this.hmm.updateHibernateConfig(this.configName, this.mappingName);
		
	}
	/**
	 * 增加多个字段
	 * @param fieldInfo
	 */
	public void addEntityField(List<FieldInfo> fieldInfo ){	
		hem.addEntityField(fieldInfo);
	}
	//拷贝的副本
	private String getMappingCopyTemplate(){
		return this.mappingCopyTemplate;
	}
	
	private String getMappingName(){
		return this.mappingName;
	}
	
	/*public PageBean getPageBean(int start,int pageSize,EduHallData ehd,String entityName){
		return pisdao.getPageRecord(start, pageSize,  ehd,entityName);
	}
	public PageBean getPageBean(int start,int pageSize,String entityName,EduHallDataForm form){
		EduHallData pojo=new EduHallData();
		BeanUtil.copyProperties(pojo, form);
		return getPageBean(start,pageSize,pojo,entityName);
	}
	public PageBean getPageBean(String entityName,EduHallDataForm form){
		int start=0;
		int pageSize=0;
		if(form.getJumpPage()!=null&&!"".equals(form.getJumpPage())){
			start=Integer.valueOf(form.getJumpPage());
		}
		if(form.getPageSize()!=null&&!"".equals(form.getPageSize())){
			pageSize=Integer.valueOf(form.getPageSize());
		}
		return getPageBean(start, pageSize, entityName, form);
	}
	
	public PageBean getPageBean(EduHallDataForm form){
		int start=0;
		int pageSize=0;
		String entityName=form.getEntityName();
		if(form.getJumpPage()!=null&&!"".equals(form.getJumpPage())){
			start=Integer.valueOf(form.getJumpPage());
		}
		if(form.getPageSize()!=null&&!"".equals(form.getPageSize())){
			pageSize=Integer.valueOf(form.getPageSize());
		}
		return getPageBean(start, pageSize, entityName, form);
	}*/
	
}
